//
//  UIViewController+MYAdd.m
//  Weiji
//
//  Created by admin on 16/7/12.
//  Copyright © 2016年 developer. All rights reserved.
//

#import "UIViewController+MYAdd.h"

#import <objc/runtime.h>
#import <YYCategories.h>
#import "Test1ViewController.h"
#import "Test2ViewController.h"

@implementation UIViewController (CardAdd)
- (void)addCardViewController:(UIViewController *)viewController {
    [self.view addSubview:viewController.view];
    [self addChildViewController:viewController];
}

- (void)cardViewControllerRemove {
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}
@end

@implementation UIViewController (MYAdd)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        // 如果 swizzling 的是类方法, 采用如下的方式:
        // Class class = object_getClass((id)self);
        // ...
        // Method originalMethod = class_getClassMethod(class, originalSelector);
        // Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
        
        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(swizzling_viewWillAppear:);
        
        Method  originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        method_exchangeImplementations(originalMethod, swizzledMethod);
    });
}

#pragma mark - Method Swizzling
- (void)swizzling_viewWillAppear:(BOOL)animated {
    [self swizzling_viewWillAppear:animated];
    if ([[self class] isSubclassOfClass:[Test1ViewController class]] ||
        [[self class] isSubclassOfClass:[Test2ViewController class]]) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    if (self.navigationController.viewControllers.count > 1) {
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(0, 0, 44, 44);
        [backButton setTitle:@"" forState:UIControlStateNormal];
        [backButton setImage:[UIImage imageNamed:@"back_icon"] forState:UIControlStateNormal];
//        [backButton setImageEdgeInsets:UIEdgeInsetsMake(0, -22, 0, 0)];
        [backButton addTarget:self action:@selector(backEvent) forControlEvents:UIControlEventTouchUpInside];
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
        [leftView addSubview:backButton];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    }
}

- (void)backEvent {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rotationAnimationOnView:(UIView *)view from:(UIView *)fromView to:(UIView *)toView {
    CGPoint toOrigin = [view convertPoint:toView.frame.origin fromView:toView.superview];
    [self rotationAnimationOnView:view fromView:fromView toOrigin:toOrigin];
}

- (void)rotationAnimationOnView:(UIView *)view fromView:(UIView *)fromView toOrigin:(CGPoint)toOrigin {
    
    CGPoint fromOrigin = [view convertPoint:fromView.frame.origin fromView:fromView.superview];
    
    UIGraphicsBeginImageContextWithOptions(fromView.frame.size, NO, 0.0);
    [fromView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    __block UIImageView *animationView = [[UIImageView alloc] initWithImage:screenshotImage];
    animationView.origin = fromOrigin;
    [view addSubview:animationView];
    
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 11.0];
    rotationAnimation.duration = 0.5f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 0;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [animationView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    });
    
    [UIView animateWithDuration:0.5f animations:^{
        animationView.origin = toOrigin;
    } completion:^(BOOL finished) {
        [animationView removeFromSuperview];
        animationView = nil;
    }];
}
@end
